package com.webmarmun.baza;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Loadxml extends Activity {
	SitesList sitesList = null;
	final int x = 0;

	public void Create() {

		setContentView(R.layout.main);
		/** Create a new textview array to display the results */
		TextView name[] = null;
		
		TextView adresa[] = null;

		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(1);

		try {

			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			/** Send URL to parse XML Tags */
			URL sourceUrl = new URL(
					"http://prvisud.rs/magicxml/baza_tumaca.xml");

			/** Create handler to handle XML Tags ( extends DefaultHandler ) */
			MyXMLHandler myXMLHandler = new MyXMLHandler();
			xr.setContentHandler(myXMLHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));

		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Get result from MyXMLHandler SitlesList Object */
		sitesList = MyXMLHandler.sitesList;

		/** Assign textview array lenght by arraylist size */
		name = new TextView[sitesList.getIme().size()];
		
		adresa = new TextView[sitesList.getIme().size()];
		

		/** Set the result text in textview and add it to layout */
		for (int i = 0; i < sitesList.getAdresa().size(); i++) {
			name[i] = new TextView(this);
			name[i].setText(sitesList.getPrezime().get(i) + " "
					+ sitesList.getIme().get(i));
			name[i].setTextSize(20);
			name[i].setHeight(20);
			name[i].setId(i);
			name[i].setClickable(true);

			name[i].setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// Intent myIntent1 = new
					// Intent(BazasudskihvestakaitumacaActivity.this,SingleView.class);
					// BazasudskihvestakaitumacaActivity.this.startActivityForResult(myIntent1,
					// name[i].getId());
				}
			});

			adresa[i] = new TextView(this);
			adresa[i].setText(sitesList.getAdresa().get(i));
			adresa[i].setHeight(20);

			layout.addView(name[i]);
			
			layout.addView(adresa[i]);

		}

		/** Set the layout view to display */
		ScrollView svh = (ScrollView) findViewById(R.id.scroll);
		svh.addView(layout);

		setContentView(svh);

	}

}
